package cn.wolfcode.shop.common;

import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.BindException;

/**
 *  组件异常处理的基类，不加@ControlerAdvice
 */
@NoArgsConstructor
public class CommonExceptionAdvice {

    //默认的提示错误信息
    private CodeMsg defaultCodeMsg = CodeMsg.DEFAULT_ERROR;

    //构造器的作用，当错误不是默认信息时，可以将它覆盖
    public CommonExceptionAdvice(CodeMsg codeMsg){
        //可以将定义的默认错误覆盖
        defaultCodeMsg = codeMsg;
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result hanlderDefaultException(BusinessException e){
        e.printStackTrace();
        return Result.error(e.getCodeMsg());
    }

}
